package com.leetcode.dp;

/**
 * @author Dennis Li
 * @date 2020/7/19 17:15
 */
public class NumSquares_279 {

    public int numSquares(int n) {
        int[] dp = new int[n + 1];
        dp[1] = 1;
        for (int i = 2; i <= n; i++) {
            dp[i] = Integer.MAX_VALUE;
            for (int j = 1; j * j <= i; j++) {
                // i = j * j 的时候，返回dp[0] + 1，dp[0]的默认值为0，直接返回1
                dp[i] = Math.min(dp[i], dp[i - j * j] + 1);
            }
        }
        return dp[n];
    }

}
